<?php function tf_ftp_transfer() {
  global $thfr_css, $thisTemplate;

  $transfertype = $_POST['transfertype'];
  $file_array = $_POST['ftp_file_array'];
  $server_address = $_POST['ftp_server_address'];
  $user_name = $_POST['ftp_user_name'];
  $user_pass = $_POST['ftp_user_pass'];
  $theme_dirname = $_POST['ftp_theme_dirname'];
  $remote_path = $_POST['ftp_remote_path'];

  if (empty($file_array) AND $transfertype != 'ftptestsubmit') {
    die("<span style='color:red'>No files selected for FTP transfer!</span>");
  }
  if ($server_address == '') {
    die("<span style='color:red'>FTP server address is missing!</span>");
  }
  if ($user_name == '') {
    die("<span style='color:red'>FTP user name is missing!</span>");
  }
  if ($user_pass == '') {
    die("<span style='color:red'>FTP user password is missing!</span>");
  }
  if ($remote_path == '') {
    die("<span style='color:red'>FTP remote path is missing!</span>");
  }

  // set up basic connection
  $conn_id = @ftp_connect($server_address);
  if (!$conn_id) {
    die("<span style='color:red'>Could not connect to FTP server !</span>");
  }

  // login with username and password
  $login_result = @ftp_login($conn_id, $user_name, $user_pass);
  if (!$login_result) {
    die("<span style='color:red'>Could not login into FTP account !</span>");
  }

  // Turn passive mode on
  ftp_pasv($conn_id, true);

  // Display current directory
  echo "<span style='color:green'>Logged in at <span style='color:blue'><code>$server_address</code></span><br />Current directory is: <span style='color:blue'><code> " . ftp_pwd($conn_id) . "</code></span><br />";

  // get contents of the current directory
  /*
  $contents = ftp_nlist($conn_id, ".");
  foreach ( $contents as $content ) {
    if ($content != '.' AND $content != '..')
    echo "<span style='color:blue'><code>" . $content . "</code></span><br />";
  }
  */

  // try to change the directory to remote directory
  if (@ftp_chdir($conn_id, $remote_path . $theme_dirname)) {
    echo "Changed into directory: <br /><span style='color:blue'><code>" . ftp_pwd($conn_id) . "</code></span><br />";
  } else {
    echo "Directory <span style='color:blue'><code>" . $remote_path . $theme_dirname . "</code></span> does not exist.<br />";
    if (@ftp_mkdir($conn_id, $remote_path . $theme_dirname)) {
      echo "Created directory: <span style='color:blue'><code>" . $remote_path . $theme_dirname . "</code></span><br />";
      if (@ftp_chdir($conn_id, $remote_path . $theme_dirname)) {
        echo "Changed into directory: <span style='color:blue'><code>" . ftp_pwd($conn_id) . "</code></span><br />";
      } else {
        echo "<span style='color:red'>Could not change to directory <code>$remote_path/$theme_dirname</code></span><br />";
      }
    } else {
      die("<span style='color:red'>Could not create <code>$theme_dirname</code> inside <code>$remote_path</code></span><br />");
    }
  }

  if ($transfertype == 'ftpsubmit') {
    // Check if /images/ directory exists
    $imgdirexists = FALSE;
    $contents = ftp_nlist($conn_id, ".");
    foreach ($contents as $content) {
      if ($content == 'images') {
        $imgdirexists = TRUE;
      }
      echo "<span style='color:blue'><code>" . $content . "</code></span><br />";
    }

    foreach ($file_array as $file_name) {

      // Image file
      if (strpos($file_name, 'images/') !== FALSE) {

        // Create /images/ directory if necessary
        if ($imgdirexists === FALSE) {
          echo "Directory <span style='color:blue'><code>" . $remote_path . $theme_dirname . "/images/</code></span>does not exist.<br />";

          if (ftp_mkdir($conn_id, ftp_pwd($conn_id) . '/images')) {
            echo "Created directory: <span style='color:blue'><code>" .
              $remote_path . $theme_dirname . "/images/</code></span><br />";
            $imgdirexists = TRUE;
          } else {
            die("<span style='color:red'>Could not create directory <code>images</code> inside <code>$remote_path" .
              "$theme_dirname</code></span><br />" . $remote_path . $theme_dirname . "/images");
            $imgdirexists = FALSE;
          }
        }
        $filename = THFRPATH . 'uploads/' . str_replace('images/', '', $file_name);
        $filename = str_replace('\\', '/', $filename);

        // try to upload $file
        if (ftp_put($conn_id, $file_name, $filename, FTP_BINARY)) {
          echo "Transferred <span style='color:blue'><code>$file_name</code></span><br />";
        } else {
          echo "<span style='color:red'>There was a problem while uploading <code>$file_name</code></span><br />";
        }
        // ASCII files
      } else {
        $thisTemplate = str_replace('.php', '', $file_name);
        if ($file_name == 'style.css') {
          $file_content = tf_print_css_code(0);
        } elseif ($file_name == 'javascript.js') {
          $file_content = tf_print_js();
        } elseif ($file_name == 'functions.php') {
          $file_content = tf_create_functions_file();
        } elseif ($file_name == 'header.php') {
          $file_content = tf_create_header_file();
        } elseif ($file_name == 'footer.php') {
          $file_content = tf_create_footer_file();
        } elseif ($file_name == 'loop.php') {
          $file_content = tf_create_loop_file();
        } else {
          $file_content = tf_create_php_file($thisTemplate);
        }

        // Create temporary file
        $temp = tmpfile();
        fwrite($temp, $file_content);
        fseek($temp, 0);

        // try to upload $file
        if (ftp_fput($conn_id, $file_name, $temp, FTP_ASCII)) {
          echo "Transferred <span style='color:blue'><code>$file_name</code></span><br />";
        } else {
          echo "<span style='color:red'>There was a problem while uploading <code>$file_name</code></span><br />";
        }
        fclose($temp); // this removes the file
      }
    }
  } else {
    // Create temporary file
    $temp = tmpfile();
    fwrite($temp, "Themeframe FTP test file: Test 1234. This file should be named text1234.txt and should have been transferred to your web hosting account, right into the direcotry of your ThemeFrame-created WP theme.");
    fseek($temp, 0);

    // try to upload $file
    if (ftp_fput($conn_id, 'test1234.txt', $temp, FTP_ASCII)) {
      echo "Successfully transferred <span style='color:blue'><code>test1234.txt</code></span><br />";
    } else {
      echo "<span style='color:red'>There was a problem while uploading <code>test1234.txt</code></span><br />";
    }
    fclose($temp); // this removes the file
  }

  // get contents of the current directory
  /*
  echo "Listing contents of current directory:<br />";
  $contents = ftp_nlist($conn_id, ".");
  foreach ( $contents as $content ) {
    if ($content != '.' AND $content != '..') {
      echo "<span style='color:blue'><code>" . $before . $content . $after . "</code></span><br />";
    }
  }
  */

  // close the connection and the file handler
  ftp_close($conn_id);

  echo "FTP connection closed.";

  $thfr_css['ftp_server_address'] = $server_address;
  $thfr_css['ftp_user_name'] = $user_name;
  $thfr_css['ftp_user_pass'] = $user_pass;
  $thfr_css['ftp_theme_dirname'] = $theme_dirname;
  $thfr_css['ftp_remote_path'] = $remote_path;

  set_thfr();
}

?>